package com.which.one;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import com.which.one.deepseek.*;

public class AnswerBookActivity extends AppCompatActivity {

    private TextView titleTextView, dateTextView, timeTextView;
    private Button generateButton;
    private List<String> answers;
    private Random randomGenerator;
    private Handler handler = new Handler();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.answer_book_activity);

        titleTextView = findViewById(R.id.titleTextView);
        dateTextView = findViewById(R.id.dateTextView);
        timeTextView = findViewById(R.id.timeTextView);
        generateButton = findViewById(R.id.generateButton);

        // Hide the title and explanation initially
        titleTextView.setVisibility(View.GONE);
        dateTextView.setVisibility(View.GONE);
        timeTextView.setVisibility(View.GONE);

        // Load answers from assets
        loadAnswers();

        // Initialize random generator
        randomGenerator = new Random();

        // Update the date and time when the activity starts
        updateDateTime();

        // Schedule updates to the date and time every second
        scheduleDateTimeUpdates();

        generateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                generateRandomAnswer();
            }
        });
    }

    private void loadAnswers() {
        answers = new ArrayList<>();
        try {
            InputStream inputStream = getAssets().open("answers.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                answers.add(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generateRandomAnswer() {
        if (answers.isEmpty()) {
            titleTextView.setText("没有可用的答案");
        } else {
            int index = randomGenerator.nextInt(answers.size());
            String answer = answers.get(index);
            // Insert newline character after each character
            StringBuilder formattedAnswer = new StringBuilder();
            for (char c : answer.toCharArray()) {
                formattedAnswer.append(c).append("\n");
            }
            titleTextView.setText(formattedAnswer.toString());
        }
        // Show the title and explanation after generating an answer
        titleTextView.setVisibility(View.VISIBLE);
        generateButton.setVisibility(View.GONE);
        dateTextView.setVisibility(View.VISIBLE);
        timeTextView.setVisibility(View.VISIBLE);
    }

    private void updateDateTime() {
        Date currentDate = new Date();
        dateTextView.setText(dateFormat.format(currentDate));
        timeTextView.setText(timeFormat.format(currentDate));
    }

    private void scheduleDateTimeUpdates() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                updateDateTime();
                scheduleDateTimeUpdates(); // Reschedule the update
            }
        }, 1000); // Update every 1000 milliseconds (1 second)
    }
}